require('glon');

local condefs = {
	GPM_Colors_Local_r = 239,
	GPM_Colors_Local_g = 239,
	GPM_Colors_Local_b = 44,
	GPM_Colors_Local_a = 255,
	
	GPM_Colors_Text_r = 50,
	GPM_Colors_Text_g = 167,
	GPM_Colors_Text_b = 120,
	GPM_Colors_Text_a = 255,
	
	GPM_Colors_FriendText_r = 120,
	GPM_Colors_FriendText_g = 167,
	GPM_Colors_FriendText_b = 50,
	GPM_Colors_FriendText_a = 255,
	
	GPM_Colors_ChatBG_r = 80,
	GPM_Colors_ChatBG_g = 80,
	GPM_Colors_ChatBG_b = 120,
	GPM_Colors_ChatBG_a = 130,
	
	GPM_Colors_HeaderText_r = 255,
	GPM_Colors_HeaderText_g = 255,
	GPM_Colors_HeaderText_b = 255,
	GPM_Colors_HeaderText_a = 150,
	
	GPM_Colors_HeaderBG_r = 73,
	GPM_Colors_HeaderBG_g = 136,
	GPM_Colors_HeaderBG_b = 205,
	GPM_Colors_HeaderBG_a = 150,
	
	GPM_Colors_HeaderBGFlash_r = 153,
	GPM_Colors_HeaderBGFlash_g = 204,
	GPM_Colors_HeaderBGFlash_b = 255,
	GPM_Colors_HeaderBGFlash_a = 150,
	
	GPM_Colors_Outline_r = 0,
	GPM_Colors_Outline_g = 0,
	GPM_Colors_Outline_b = 0,
	GPM_Colors_Outline_a = 255,
	
	GPM_Colors_TextEntryBG_r = 0,
	GPM_Colors_TextEntryBG_g = 0,
	GPM_Colors_TextEntryBG_b = 0,
	GPM_Colors_TextEntryBG_a = 180,
	
	GPM_Colors_TextEntryCursor_r = 255,
	GPM_Colors_TextEntryCursor_g = 255,
	GPM_Colors_TextEntryCursor_b = 255,
	GPM_Colors_TextEntryCursor_a = 180,
	
	GPM_Colors_TextEntryText_r = 255,
	GPM_Colors_TextEntryText_g = 255,
	GPM_Colors_TextEntryText_b = 255,
	GPM_Colors_TextEntryText_a = 180
}

local convars = {}
GPM.ConVars = {}

function GPM.SaveColors()
	local tab = GPM.Colors;
	local savestr = glon.encode(tab);
	file.Write("GPM/GUI/Colors.txt", savestr);
end
 
function GPM.LoadColors()
	local loadstr = file.Read("GPM/GUI/Colors.txt");
	local tab = glon.decode(loadstr);
	if not (tab and table.Count(tab) > 0) then
		tab = condefs;
	end
	for k, v in pairs(tab) do
		if type(v) == "number" then
			GPM.ConVars[ string.sub( k, 12 ) ] = CreateClientConVar( k, v, false, true );
			convars[#convars+1] = k;
			cvars.AddChangeCallback(k, GPM.SaveColors);
		else
			for i, c in pairs(v) do
				GPM.ConVars[k.."_"..i] = CreateClientConVar( "GPM_Colors_"..k.."_"..i, c, false, true );
				convars[#convars+1] = "GPM_Colors_"..k.."_"..i;
				cvars.AddChangeCallback("GPM_Colors_"..k.."_"..i, GPM.SaveColors);
			end
		end
		
	end
end

local function CreateColorMenu(panel)
 	panel:AddControl("ComboBox", {
		Options = { ["Default"] = condefs },
		CVars = convars,
		Label = "",
		MenuButton = "1",
		Folder = "GPM"
	}) 
	
	panel:AddControl("Label", {Text = ""})
	
	panel:AddControl("Label", {
		Text = "Colors:"
	})
	
	panel:AddControl("Label", {
		Text = "Your chat text color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_Local_r",
		Green = "GPM_Colors_Local_g",
		Blue = "GPM_Colors_Local_b",
		Alpha = "GPM_Colors_Local_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Others chat text color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_Text_r",
		Green = "GPM_Colors_Text_g",
		Blue = "GPM_Colors_Text_b",
		Alpha = "GPM_Colors_Text_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Steam Friends chat text color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_FriendText_r",
		Green = "GPM_Colors_FriendText_g",
		Blue = "GPM_Colors_FriendText_b",
		Alpha = "GPM_Colors_FriendText_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Header text color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_HeaderText_r",
		Green = "GPM_Colors_HeaderText_g",
		Blue = "GPM_Colors_HeaderText_b",
		Alpha = "GPM_Colors_HeaderText_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Chat Background color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_ChatBG_r",
		Green = "GPM_Colors_ChatBG_g",
		Blue = "GPM_Colors_ChatBG_b",
		Alpha = "GPM_Colors_ChatBG_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Header Background color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_HeaderBG_r",
		Green = "GPM_Colors_HeaderBG_g",
		Blue = "GPM_Colors_HeaderBG_b",
		Alpha = "GPM_Colors_HeaderBG_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Header Alternative color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_HeaderBGFlash_r",
		Green = "GPM_Colors_HeaderBGFlash_g",
		Blue = "GPM_Colors_HeaderBGFlash_b",
		Alpha = "GPM_Colors_HeaderBGFlash_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Outline color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_Outline_r",
		Green = "GPM_Colors_Outline_g",
		Blue = "GPM_Colors_Outline_b",
		Alpha = "GPM_Colors_Outline_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Text Entry Background color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_TextEntryBG_r",
		Green = "GPM_Colors_TextEntryBG_g",
		Blue = "GPM_Colors_TextEntryBG_b",
		Alpha = "GPM_Colors_TextEntryBG_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Text Entry Cursor color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_TextEntryCursor_r",
		Green = "GPM_Colors_TextEntryCursor_g",
		Blue = "GPM_Colors_TextEntryCursor_b",
		Alpha = "GPM_Colors_TextEntryCursor_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
	
	panel:AddControl("Label", {
		Text = "Text Entry Text color"
	})
	
	panel:AddControl("Color", {
		Red = "GPM_Colors_TextEntryText_r",
		Green = "GPM_Colors_TextEntryText_g",
		Blue = "GPM_Colors_TextEntryText_b",
		Alpha = "GPM_Colors_TextEntryText_a",
		ShowAlpha = true,
		ShowRGB = true,
		ShowHSV = true,
		Multiplier = 255
	})
end

hook.Add( "PopulateToolMenu", "GPMMenu", function( )
	spawnmenu.AddToolMenuOption("Options", "GPM", "GPM_Colors", "Colors", "", "", CreateColorMenu);
end );

hook.Add("Initialize", "GPM.Hooks.Initialize", GPM.LoadColors());